package com.lepu.app.fun.my;

import java.util.ArrayList;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;

import com.app.utils.LoginUtil;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.main.UIHelper;
import com.eyzhs.app.R;
import com.lepu.app.fun.my.adapter.AdapterCollectArticleList;
import com.lepu.app.fun.my.adapter.AdapterCollectKnowledge;
import com.lepu.app.fun.my.adapter.AdapterQuestionList;
import com.lepu.app.fun.my.bean.BeanCollectArticle;
import com.lepu.app.fun.my.bean.BeanCollectKnowledge;
import com.lepu.app.fun.my.bean.BeanQuestionAnswer;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;
import com.lepu.app.widget.ListViewForScrollview;

public class CollectKnowledgeActivity extends BaseFragmentActivity implements
		OnTopbarNewLeftLayoutListener {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_collect_knowledge_activity);

		init();
	}

	@Override
	public void onResume() {
		super.onResume();

		updateCollectData();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	/**
	 * 添加页面动画，需要重写这个方法，
	 */
	@Override
	public void onBackPressed() {
		finish(true);
	}

	/**
	 * 初始化方法
	 */
	private void init() {
		// topbar
		CustomTopBarNew topbar = (CustomTopBarNew) findViewById(R.id.topbar);
		topbar.setonTopbarNewLeftLayoutListener(this);

		topbar.setTopbarTitle("已收藏的知识");
	}
	
	private void showHideNoDataLayout(boolean isShow)
	{
		LinearLayout noDataLayout = (LinearLayout) findViewById(R.id.noDataLayout);
		if(noDataLayout != null)
		{
			if(isShow)
			{
				noDataLayout.setVisibility(View.VISIBLE);
			}else{
				noDataLayout.setVisibility(View.GONE);
			}
		}
	}
	
	@SuppressWarnings("unused")
	private void testInsertTiezi()
	{
		BeanCollectKnowledge item = new BeanCollectKnowledge();
		item.Uid = LoginUtil.getUserId();
		item.Time = System.currentTimeMillis();

		item.Type = BeanCollectKnowledge.TYPE_TIEZI;
		item.Title = "test";
		item.ArticleID = "10";
		BeanCollectKnowledge.insertCollectItem(item);
	}

	private void updateCollectData() {
		
		ArrayList<BeanCollectKnowledge> dataList = BeanCollectKnowledge
				.getAllCollect(LoginUtil.getUserId());

		if (dataList == null || dataList.size() == 0) {
			UIHelper.showToast(this, "您当前没有收藏的内容");
		}
		ScrollView contentScrollView = (ScrollView) findViewById(R.id.contentScrollView);
		contentScrollView.setVisibility(View.VISIBLE);

		LinearLayout videoLayout = (LinearLayout) findViewById(R.id.videoLayout);
		LinearLayout articleLayout = (LinearLayout) findViewById(R.id.articleLayout);
		LinearLayout questionLayout = (LinearLayout) findViewById(R.id.questionLayout);
		LinearLayout tieziLayout = (LinearLayout) findViewById(R.id.tieziLayout);

		ListViewForScrollview videoListView = (ListViewForScrollview) findViewById(R.id.videoListView);
		ListViewForScrollview articleListView = (ListViewForScrollview) findViewById(R.id.articleListView);
		ListViewForScrollview questionListView = (ListViewForScrollview) findViewById(R.id.questionListView);
		ListViewForScrollview tieziListView = (ListViewForScrollview) findViewById(R.id.tieziListView);

		ArrayList<BeanCollectKnowledge> videoList = new ArrayList<BeanCollectKnowledge>();
		ArrayList<BeanCollectKnowledge> articleList = new ArrayList<BeanCollectKnowledge>();
		ArrayList<BeanCollectKnowledge> questionList = new ArrayList<BeanCollectKnowledge>();
		ArrayList<BeanCollectKnowledge> tieziList = new ArrayList<BeanCollectKnowledge>();

		if (dataList != null && dataList.size() > 0) {
			showHideNoDataLayout(false);
			for (BeanCollectKnowledge item : dataList) {
				int type = item.Type;
				if (type == BeanCollectKnowledge.TYPE_VIDEO_NEW) {
					videoList.add(item);
				} else if (type == BeanCollectKnowledge.TYPE_ARTICLE_NEW ||
						type == BeanCollectKnowledge.TYPE_ARTICLE_WRITER) {
					articleList.add(item);
				} else if (type == BeanCollectKnowledge.TYPE_QUESTION) {
					questionList.add(item);
				} else if (type == BeanCollectKnowledge.TYPE_TIEZI) {
					tieziList.add(item);
				}
			}
		}else{
			showHideNoDataLayout(true);
		}

		// video
		if (videoList.size() > 0) {
			videoLayout.setVisibility(View.VISIBLE);
		} else {
			videoLayout.setVisibility(View.GONE);
		}
		videoListView.setAdapter(new AdapterCollectKnowledge(this, videoList));

		// article
		ArrayList<BeanCollectArticle> collectArticleList = new ArrayList<BeanCollectArticle>();
		if (articleList.size() > 0) {
			articleLayout.setVisibility(View.VISIBLE);

			for (BeanCollectKnowledge item : articleList) {
				BeanCollectArticle article = new BeanCollectArticle();
				article.Type = item.Type;
				article.Title = item.Title;
				article.Content = item.SubTitle;
				article.ArticleID = item.ArticleID;
				collectArticleList.add(article);
			}
		} else {
			articleLayout.setVisibility(View.GONE);
		}
		articleListView.setAdapter(new AdapterCollectArticleList(this,
				collectArticleList));

		// question
		ArrayList<BeanQuestionAnswer> collectQuestionList = new ArrayList<BeanQuestionAnswer>();
		if (questionList.size() > 0) {
			questionLayout.setVisibility(View.VISIBLE);

			for (BeanCollectKnowledge item : questionList) {
				BeanQuestionAnswer question = new BeanQuestionAnswer();
				question.ARQuestion = item.Title;
				question.ARAnswer = item.SubTitle;
				collectQuestionList.add(question);
			}
		} else {
			questionLayout.setVisibility(View.GONE);
		}
		questionListView.setAdapter(new AdapterQuestionList(
				CollectKnowledgeActivity.this, collectQuestionList));

		// tiezi
		ArrayList<BeanCollectArticle> collectTieziList = new ArrayList<BeanCollectArticle>();
		if (tieziList.size() > 0) {
			tieziLayout.setVisibility(View.VISIBLE);

			for (BeanCollectKnowledge item : tieziList) {
				BeanCollectArticle article = new BeanCollectArticle();
				article.Type = item.Type;
				article.Title = item.Title;
				article.Content = item.SubTitle;
				article.ArticleID = item.ArticleID;
				collectTieziList.add(article);
			}
		} else {
			tieziLayout.setVisibility(View.GONE);
		}
		tieziListView.setAdapter(new AdapterCollectArticleList(this,
				collectTieziList));
	}

	@Override
	public void onTopbarLeftLayoutSelected() {
		finish(true);
	}

}
